package com.aaa.qy127.service.impl;

import com.aaa.qy127.dao.DlDao;
import com.aaa.qy127.dao.StaffDao;
import com.aaa.qy127.entity.Dl;
import com.aaa.qy127.entity.Staff;
import com.aaa.qy127.service.DlService;
import com.aaa.qy127.util.CodeAndMsg;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service("DlServiceImpl")
public class DlServiceImpl implements DlService {
    @Resource(name = "DlDaoImpl")
    private DlDao dlDao;

    /**
     * 通过 phone和 password 获取 dl 的信息
     * 如果 staff 中没有查询到值 就返回一个code“1002” 表示 用户名或者密码错误
     * 否则 返回一个code “1001” 表示可以登录
     *
     * @param phone
     * @param password
     * @return
     */
    @Override
    public CodeAndMsg getCodeAndMsg(String phone, String password, HttpServletRequest request) {
        Dl dl = dlDao.getphoneAndpassword(phone, password);
        CodeAndMsg codeAndMsg = new CodeAndMsg();
        if (dl == null) {
            codeAndMsg.setCode(1002);
            codeAndMsg.setMsg("用户名或者密码错误");

        }else if(dl.getStatus()==1){
            codeAndMsg.setCode(1003);
            codeAndMsg.setMsg("此账号已禁用");
        } else {
            request.getSession().setAttribute("dl", dl);
            codeAndMsg.setCode(1001);
        }
        return codeAndMsg;
    }

    @Override
    public CodeAndMsg InsDl(Dl dl) {
        Integer userid = dl.getUserid();
        CodeAndMsg codeAndMsg = new CodeAndMsg();
        if (userid == null) {
            int i = dlDao.insertOneDl(dl);
            if (i > 0) {
                codeAndMsg.setCode(1001);
                codeAndMsg.setMsg("新增成功");
            } else {
                codeAndMsg.setCode(1002);
                codeAndMsg.setMsg("新增失败");
            }

        }
        return codeAndMsg;

    }

    /**
     * 向 用户表中插入一条数据
     * 先判断 用户表中 是否 已经存在
     * 存在 就不执行插入 操作
     * 不存在 就执行 插入 操作
     * @param dl
     * @return
     */
    @Override
    public CodeAndMsg zhuce(Dl dl,HttpServletRequest request) {
        int yanzhengma = (int) request.getSession().getAttribute("yanzhengma");
        CodeAndMsg codeAndMsg = new CodeAndMsg();
        if (yanzhengma == dl.getYanzhengma()){
            List<Map<String, Object>> oneUserByCardIDAndPhone = dlDao.getOneUserByCardIDAndPhone(dl);
            if (oneUserByCardIDAndPhone == null){
                int i = dlDao.insertDL(dl);
                if (i>0){
                    codeAndMsg.setCode(1001);
                    codeAndMsg.setMsg("注册成功");
                }else {
                    codeAndMsg.setCode(1002);
                    codeAndMsg.setMsg("注册失败");
                }
            }else {
                codeAndMsg.setCode(1003);
                codeAndMsg.setMsg("已存在");
            }
        }else {
            codeAndMsg.setCode(1005);
            codeAndMsg.setMsg("验证码不正确");
        }
        return codeAndMsg;
    }

    /**
     * 通过 userid  修改 该用户 信息
     * 然后重新 根据 该用户的 userid 查询 一遍
     * @param dl
     * @return
     */
    @Override
    public CodeAndMsg updateOneUserByUserid(Dl dl,HttpServletRequest request) {
        int i = dlDao.updateOneUserByUserid(dl);
        CodeAndMsg codeAndMsg = new CodeAndMsg();
        HttpSession session = request.getSession();
        Dl dl1 = (Dl) session.getAttribute("dl");
        Dl oneUserByUserid = dlDao.getOneUserByUserid(dl1.getUserid());
        session.setAttribute("dl",oneUserByUserid);

        if (i>0){
            codeAndMsg.setCode(1001);
            codeAndMsg.setMsg("修改成功");
        }else {
            codeAndMsg.setCode(1002);
            codeAndMsg.setMsg("修改失败");
        }
        return codeAndMsg;
    }

    /**
     * 通过 该用户的 userid 修改 该用户的 密码
     * @param password
     * @param userid
     * @return
     */
    @Override
    public CodeAndMsg updatePasswordByUserid(String password, String userid) {
        int i = dlDao.updatePasswordByUserid(password, userid);
        CodeAndMsg codeAndMsg = new CodeAndMsg();
        if (i>0){
            codeAndMsg.setCode(1001);
            codeAndMsg.setMsg("修改成功");
        }else {
            codeAndMsg.setCode(1002);
            codeAndMsg.setMsg("修改失败");
        }
        return codeAndMsg;
    }
}